package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.vo.Job;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <pre>
 * 分析任务
 * Created by clb on 17-3-17.
 * </pre>
 */
@Api(description = "分析作业")
@RestController
@RequestMapping("/jobs")
public class JobController {
    @Autowired
    private JobService jobService;

    @ApiOperation(value = "查询(带分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "taskId", value = "按照任务查询", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "modelId", value = "模型id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "jobId", value = "作业id", dataType = "Long", paramType = "query")
    })
    @GetMapping(value = "")
    public ResponseEntity<CommonResultResponse> page(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                     @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                     @RequestParam(value = "taskId", required = false) Long taskId,
                                                     @RequestParam(value = "modelId", required = false) Long modelId,
                                                     @RequestParam(value = "jobId", required = false) Long jobId) throws Exception {

        int count = jobService.selectCount(taskId,modelId,jobId);
        pageSize = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
        pageNum = (pageNum == null) ? Integer.valueOf(1) : pageNum;
        PageHelper.startPage(pageNum, pageSize);
        PageInfo pageInfo = new PageInfo(pageNum, pageSize, count);
        List<Job> all = jobService.listJobs2(taskId,modelId,pageNum,pageSize,jobId);
        return ResponseHelper.success(all,pageInfo);
    }

    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> jobById(@PathVariable(value = "id", required = false) Long id) throws Exception {
        return ResponseHelper.success(jobService.get(id));
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobId", value = "作业编号", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "projId", value = "projId", required = true, dataType = "Long", paramType = "query")
    })
    @DeleteMapping(value = "")
    public ResponseEntity<CommonResultResponse> delete(@RequestParam Long jobId,
                                                       @RequestParam Long projId) throws Exception {
        return ResponseHelper.success(jobService.delete(projId,jobId));
    }

    @ApiOperation(value = "根据jobId更新状态,处理分析结果")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "作业编号", required = true, dataType = "Long", paramType = "path"),
        @ApiImplicitParam(name = "status", value = "状态:success,failed", required = false, dataType = "String", paramType = "query")
    })
    @PostMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> updateStatusByJobId(@PathVariable("id") Long id,
                                                                    @RequestParam("status") String status) throws Exception {

        return ResponseHelper.success(jobService.updateStatus(id,status));
    }

    @ApiOperation(value = "作业接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "作业编号", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "jobId", value = "作业id", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "projectId", value = "项目编号", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "deviceNo", value = "设备编号", required = false, dataType = "String", paramType = "query")
    })
    @GetMapping(value = "/{id}/interfaces")
    public ResponseEntity<CommonResultResponse> getInterfaces(@PathVariable(value = "id", required = false) Long id,
                                                              @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                              @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                              @RequestParam(value = "projectId",required = false) Long projectId,
                                                              @RequestParam(value = "deviceNo",required = false) String deviceNo) throws Exception {
        List list = jobService.listResultData(projectId,id);
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        int count = list.size();
        PageInfo pageInfo = new PageInfo(pageNum, pageSize, count);
        return ResponseHelper.success(list,pageInfo);
    }

}
